from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

import json   
import random
import dataapi.models as m

######################
def getMaxStock(level):
    if level==0:
        return 6
    if level==1:
        return 15
    if level==2:
        return  -1
    return 0
######################
#
def index(request):
    return HttpResponse("hi,youyi!")

#
def register(request):
    querydata=json.loads(request.body.decode())
    ret={'RetCode':'-1'}
    if  querydata.get('Phonenum')\
     and querydata.get('Password') :        
        acc=m.user.objects.filter(phoneNumber=querydata['Phonenum'])  
        if len(acc)==0 :
            m.user.objects.create(phoneNumber=querydata['Phonenum'],\
                             password=querydata['Password'],\
                             level=0,\
                             endDate="2100-01-01",\
                             )
            ret['RetCode']='0'
        else:
            ret['RetCode']='1'
    return HttpResponse(json.dumps(ret), content_type="application/json")    


def login(request):
    querydata=json.loads(request.body.decode())
    ret={'RetCode':'-1'}
    if  querydata.get('Phonenum')\
     and querydata.get('Password') :        
        acc=m.user.objects.filter(phoneNumber=querydata['Phonenum'])  
        scc=m.subscription.objects.filter(phoneNumber=querydata[Phonenum])
        if len(acc)==1 :            
            ret['RetCode']='0'
            ret['VIPLevel']=str(acc[0].level)
            ret['EndDate']=str(acc[0].endDate)
            ret['MaxStock']=str(getMaxStock(acc[0].level))
            ret['CurrentSubscribe']=str(len(scc))
    return HttpResponse(json.dumps(ret), content_type="application/json")  


def subscriptions(request):
    querydata=json.loads(request.body.decode())
    ret={'RetCode':'-1'}
    if  querydata.get('Phonenum')\
     and querydata.get('Password') :        
        acc=m.user.objects.filter(phoneNumber=querydata['Phonenum'])  
        scc=m.subscription.objects.filter(phoneNumber=querydata['Phonenum'])
        if len(acc)==1 :   
            data=[]
            for s in scc:
                pcc=m.predict.objects.filter(stockNum=s.stockNum)
                if len(pcc)==0:
                    continue
                tem={}
                tem['StockNum']=s.stockNum
                tem['Name']=s.name.decode()
                tem['PredictResult']=pcc[0].predict
                data.append[tem]                
            ret['RetCode']='0'
            ret['Subscriptions']=data
    return HttpResponse(json.dumps(ret), content_type="application/json")  


def historydata(request):
    querydata=json.loads(request.body.decode())
    ret={'RetCode':'-1'}
    if  querydata.get('StockNum') :        
        hcc=m.historydata.objects.filter(stockNum=querydata['StockNum'])  
        History=[]
        for h in hcc:
            HistoryPrediceData={}            
            HistoryPrediceData['Date']=h.Date
            HistoryPrediceData['PredictResult']=h.predict
            HistoryPrediceData['RealResult']=h.real
            HistoryPrediceData['openPrice']=h.openPrice
            HistoryPrediceData['closePrice']=h.closePrice
            History.append(historydata)
        if len(History)==1 :            
            ret['RetCode']='0'
            ret['StockNum']=querydata['StockNum']
            ret['Name']=str(hcc[0].name.decode())
    return HttpResponse(json.dumps(ret), content_type="application/json")  


def addSucscribe(request):
    querydata=json.loads(request.body.decode())
    ret={'RetCode':'-1'}
    if  querydata.get('Phonenum')   and querydata.get('Password') and querydata.get('StockNum') :        
        ucc=m.user.objects.filter(Phonenum=querydata['Phonenum'])  
        if len(ucc)>0:
            if ucc[0].password.decode()==querydata.get('Password'):
                scc=m.subscription.objects.filter(Phonenum=querydata['Phonenum'],
                                                  stockNum=querydata['StockNum'])  
                if len(scc)==0:
                    m.subscription.objects.create(Phonenum=querydata['Phonenum'],stockNum=querydata['StockNum'])
                ret['RetCode']='0'        
    return HttpResponse(json.dumps(ret), content_type="application/json")  